import requestProxy from '@/utils/request';
import { decrypt } from '@/utils/secret';
import { aesEncode } from '@/utils/string';
import math from '@/utils/math';
import { forEachFormOptions } from '@/utils/formMakeUtils.js';

const { request } = requestProxy;

// 通用审批-tab列表
export function getCommonTab(data) {
  return request({
    url: `/admin/common/approval/tabs`,
    method: 'post',
    data
  });
}

// 通用审批-列表
export function getCommonApprovalList(data, cancelToken) {
  return request({
    url: `/admin/common/approval/list`,
    method: 'post',
    data,
    cancelToken
  });
}

// 通用审批-列表筛选数据
export function getCommonApprovalFilterList(id) {
  return request({
    url: `/admin/common/approval/filterList/${id}`,
    method: 'get'
  });
}

// 通用审批-列表-权限判断
export function getCommonApprovalPermissions(id) {
  return request({
    url: `/admin/common/approval/permissions/${id}`,
    method: 'get'
  });
}

// 通用审批-列表-权限判断
export function getCommonApprovalModuleName(id) {
  return request({
    url: `/admin/common/approval/moduleName/${id}`,
    method: 'get'
  });
}

// 通用审批-列表删除
export function commonApprovalListDelete(id) {
  return request({
    url: `/admin/common/approval/delete/${id}`,
    method: 'delete'
  });
}

// 通用审批-创建获取配置详情
export function getApprovalTemplate(params) {
  return request({
    url: 'admin/form/process/detail',
    method: 'get',
    params
  });
}

// 表单提交、获取审批流
export function formSubmit(data) {
  return request({
    url: `/admin/common/approval/formSubmit`,
    method: 'post',
    data
  });
}
// 发起审批
export function createApproval(data) {
  return request({
    url: `/admin/common/approval/create`,
    method: 'post',
    data
  });
}

// 详情
export function approvalDetail(params) {
  return new Promise((resolve, reject) => {
    // decrypt, aesEncode
    request({
      url: `/admin/common/approval/detail`,
      method: 'get',
      params
    }).then(res => {
      if (res.data && res.data.list) {
        /**
         * 作用:
         * 1、如果加密字段是百分比字段，则先进行解密后，乘以10，然后在加密
         * 2、把普通百分比字段乘以10
         */
        forEachFormOptions(res.data.list, (element, subformKey, parentSubform) => {
          const isShowPercentage = element.type === 'input' && element.options.isShowPercentage;
          if (element.options && element.options.isSecret && isShowPercentage) {
            if (parentSubform && parentSubform.value) {
              // 子表单
              parentSubform.value.forEach(subform => {
                const value = decrypt(subform[element.key], element);
                if (value) subform[element.key] = aesEncode(math.multiply(value, 100));
              });
            } else {
              const value = decrypt(element.value, element);
              if (value) element.value = aesEncode(math.multiply(value, 100));
            }
          } else if (isShowPercentage) {
            if (element.value && typeof element.value === 'number') element.value = math.multiply(element.value, 100);
            if (parentSubform && parentSubform.value) {
              // 子表单字段处理
              parentSubform.value.forEach(val => {
                if (val[element.key] && typeof val[element.key] === 'number') val[element.key] = math.multiply(val[element.key], 100);
              });
            }
          }
        });
      }
      resolve(res);
    }).catch(err => {
      reject(err);
    });
  });
}

// 流程审批
export function approvalProcessAudit(data) {
  return request({
    url: '/admin/common/approval/process/audit',
    method: 'post',
    data
  });
}
// 流程 - 撤销
export function approvalProcessRevocation(data) {
  return request({
    url: '/admin/common/approval/process/revocation',
    method: 'post',
    data
  });
}
// 撤回
export function approvalProcessRecall(data) {
  return request({
    url: '/admin/common/approval/process/recall',
    method: 'post',
    data
  });
}
// 发表评论
export function approvalCommentAdd(data) {
  return request({
    url: `/admin/customize_form/comments/add`,
    method: 'post',
    data
  });
}
// 评论列表
export function getApprovalComments(params) {
  return request({
    url: `/admin/customize_form/comments/list`,
    method: 'get',
    params
  });
}

export function editEvent(data) {
  return request({
    url: '/admin/common/approval/event/edit',
    method: 'put',
    data
  });
}

export function mathFormulaApi(data, cancelToken) {
  return request({
    url: '/admin/form/process/formula',
    method: 'post',
    data,
    cancelToken
  });
}

// 子表单自动填充上次填写
export function subformAutoFillIn(params) {
  return request({
    url: 'admin/common/approval/last/subform/content',
    method: 'get',
    params
  });
}

export function fieldFilter(data, cancelToken) {
  return request({
    url: '/admin/common/approval/filter',
    method: 'post',
    data,
    cancelToken
  });
}

export function getApprovalExportList(id) {
  return request({
    url: `/admin/common/approval/exportList/${id}`,
    method: 'get'
  });
}

// 自定义表单-查看加密后的值
export function checkField(params) {
  return request({
    url: 'admin/form/process/check',
    method: 'get',
    params
  });
}
export function checkFieldLog(params) {
  return request({
    url: 'admin/form/process/check/log',
    method: 'get',
    params
  });
}
// 表单敏感词校验
export function sensitiveWordsCheck(data) {
  return request({
    url: `/admin/common/approval/wordsCheck`,
    method: 'post',
    data
  });
}
// 获取列表元素数据
export function getRowData(params) {
  return request({
    url: 'admin/common/approval/rowData',
    method: 'get',
    params
  });
}
// 获取列表视图配置
export function getViewConfig(params) {
  return request({
    url: 'admin/common/approval/viewConfig',
    method: 'get',
    params
  });
}
